﻿using _IMES.DBUtils;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows;
using houseAgency.Models;
using System.ComponentModel;
using System.Reflection;

namespace houseAgency.ViewModel.OutTenants
{
    public class FavorViewModel : ObservableObject
    {
        public FavorViewModel()
        {
            FavorList = new();
            FavorInfo = new();
            FavorList = GetAllFavor();
        }

        #region 属性
        private List<Favor> favorList;

        public List<Favor> FavorList
        {
            get { return favorList; }
            set
            {
                favorList = value;
                OnPropertyChanged();
            }
        }

        private Favor favorInfo;

        public Favor FavorInfo
        {
            get { return favorInfo; }
            set
            {
                favorInfo = value;
                OnPropertyChanged();
            }
        }
        private bool oKButtonEnable;

        public bool OKButtonEnable
        {
            get { return oKButtonEnable; }
            set
            {
                oKButtonEnable = value;
                OnPropertyChanged();
            }
        }
        private string btnMessage = string.Empty;

        public string BtnMessage
        {
            get { return btnMessage; }
            set
            {
                btnMessage = value;
                OnPropertyChanged();
            }
        }




        #endregion

        #region 命令 Command
        public ICommand SelectedItemChangedCommand
        {
            get
            {
                return new RelayCommand<Favor>(o =>
                {
                    if (o != null)
                    {
                        OKButtonEnable = false;
                        FavorInfo = o;
                    }
                });
            }
        }
        public ICommand AddCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    FavorInfo = new Favor() { ID = GetFavorID(), Name = "", Remake = "" };
                    OKButtonEnable = true;
                    BtnMessage = "Add";
                });
            }
        }

        public ICommand ChangeCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    if (FavorInfo.ID != null && FavorInfo.Name != null && FavorInfo.ID != "" && FavorInfo.Name != "")
                    {
                        OKButtonEnable = true;
                        BtnMessage = "Change";
                    }
                    else
                    {
                        MessageBox.Show("请选择要修改的朝向信息！！");
                    }

                });
            }
        }
        public ICommand DelCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    if (FavorList.Where(t => t.ID == FavorInfo.ID).ToArray().Length != 0)
                    {
                        if (MessageBox.Show(String.Format("您确定要删除编号为：{0}的朝向信息吗？", FavorInfo.ID), "删除操作", MessageBoxButton.OKCancel, MessageBoxImage.Question) == MessageBoxResult.OK)
                        {
                            bool result = DeleteFavor(FavorInfo.ID ?? "");
                            if (result)
                                FavorList = GetAllFavor();
                        }
                    }
                    else
                    {
                        MessageBox.Show("要删除的朝向信息不存在！！");
                    }

                });
            }
        }
        public ICommand RefreshCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    FavorList = GetAllFavor();
                });
            }
        }

        public ICommand OKCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    bool result = false;
                    if (BtnMessage == "Add")
                    {
                        result = AddFavorInfo();
                    }
                    else if (BtnMessage == "Change")
                    {
                        result = ChangeFavorInfo();
                    }
                    if (result)
                        FavorList = GetAllFavor();
                });
            }
        }
        #endregion

        #region 方法
        private List<Favor> GetAllFavor()
        {
            string sql = "select id,name,remake from favor order by id";
            DataTable gpParaTable = MySQLHelper.ExecuteDataTable(MySQLHelper.DBConnectionString, sql);
            return DBConvert.DataTableToList<Favor>(gpParaTable);
        }
        private bool AddFavorInfo()
        {
            string sql = "insert into favor(id,name,remake) values(@id,@name,@remake)";
            int result = MySQLHelper.ExecuteNonQuery(MySQLHelper.DBConnectionString, sql, new MySqlParameter[] {
                new MySqlParameter("id",FavorInfo.ID), new MySqlParameter("name",FavorInfo.Name), new MySqlParameter("remake",FavorInfo.Remake)});
            if (result > 0)
            {
                MessageBox.Show("添加成功");
                FavorInfo = new Favor() { ID = "", Name = "", Remake = "" };
                OKButtonEnable = false;
                return true;
            }
            MessageBox.Show("添加失败");
            return false;
        }

        private bool ChangeFavorInfo()
        {
            string sql = "update favor set name = @name,remake = @remake where id = @id";
            int result = MySQLHelper.ExecuteNonQuery(MySQLHelper.DBConnectionString, sql, new MySqlParameter[] {
                new MySqlParameter("name",FavorInfo.Name), new MySqlParameter("remake",FavorInfo.Remake), new MySqlParameter("id",FavorInfo.ID)});
            if (result > 0)
            {
                MessageBox.Show("修改成功");
                FavorInfo = new Favor() { ID = "", Name = "", Remake = "" };
                OKButtonEnable = false;
                return true;
            }
            MessageBox.Show("修改失败");
            return false;
        }
        private bool DeleteFavor(string ID)
        {
            string sql = "delete from favor where id = @id";
            int row = MySQLHelper.ExecuteNonQuery(MySQLHelper.DBConnectionString, sql, new MySqlParameter("id", ID));
            if (row > 0)
            {
                MessageBox.Show("删除成功");
                FavorInfo = new Favor() { ID = "", Name = "", Remake = "" };
                OKButtonEnable = false;
                return true;
            }
            MessageBox.Show("删除成功");
            return false;
        }
        private string GetFavorID()
        {
            string id = "fav";
            string sql = "select MAX(id) from favor";
            string maxId = MySQLHelper.ExecuteScalar<string>(MySQLHelper.DBConnectionString, sql);
            if (maxId != "")
            {
                id += (int.Parse(maxId.Substring(3)) + 1).ToString();
            }
            else
            {
                id += 1001;
            }
            return id;
        }
        #endregion
    }
}
